package com.gibs.peixunapi.dao;

import com.gibs.peixunapi.model.UserAndRole;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserAndRoleDao extends JpaRepository<UserAndRole, Integer>, JpaSpecificationExecutor<UserAndRole> {

	@Query("from UserAndRole where userId = ?1")
	List<UserAndRole> find(String name);


	@Query(value = "select * from UserAndRole limit ?, ?", nativeQuery = true)
	List<UserAndRole> findPage(Integer offset, Integer size);

	@Query(value = "select * from UserAndRole where UserId = ? limit ?, ?", nativeQuery = true)
	List<UserAndRole> findPage(String name, Integer offset, Integer size);

	@Query(value = "select * from UserAndRole where UserId like ? limit ?, ?", nativeQuery = true)
	List<UserAndRole> findPageLike(String name, Integer offset, Integer size);

	@Query(value = "select count(*) from UserAndRole where UserId = ?", nativeQuery = true)
	Long count(String name);

	@Query(value = "select count(*) from UserAndRole where UserId like ?", nativeQuery = true)
	Long countLike(String name);
	
	@Query("from UserAndRole order by id asc")
	List<UserAndRole> findAllAsc();

	List<UserAndRole> findUserAndRolesById(Integer id);

	@Query("select userId from UserAndRole where roleId = ?1")
	List<Integer> findUserIdListById(List<Integer> id);

}

